//
//  ChangedReplaceView.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/12/11.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

import WebKit


class ChangedReplaceView: WKBaseViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let urlStr = duihuanArray.lastObject
        
        let url = URL.init(string: urlStr as! String)
        
        
        DispatchQueue.main.async {
            
            TestNet.shared.getNetStatus(comfun: { (result) in
                if result.rawValue == "worked" {
                    print("worked")
                    
                    DispatchQueue.main.async {
                        
                        self.urlRequestCache = NSURLRequest(url: url!, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 10)
                        self.webView.load(self.urlRequestCache as URLRequest)
                        
                    }
                    
                } else {
                    
                    DispatchQueue.main.async {
                        
                        print("本地")
                        print(self.webView.bounds)
                        
                        self.urlRequestCache = NSURLRequest.init(url: url!, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 0)
                        self.webView.load(self.urlRequestCache as URLRequest)
                        
                    }
                    
                }
                
            })
            
        }
        
    }
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        
        url = (navigationAction.request.url?.absoluteString)!
        
        if navigationAction.navigationType == WKNavigationType.linkActivated {
            
            //如果为本身或者本身携带#的url进行自身刷新。
            if url == changeRocordURL {
                
                //自刷新
                self.webView.load(URLRequest.init(url: URL.init(string: url)!))
                
                duihuanArray.removeAllObjects()
                
            } else {
                
                print(duihuanArray)
                
                //取出本地token，进行拼接
                if (localSave.object(forKey: userToken) != nil) {
                    let token = localSave.object(forKey: userToken) as! String
                    
                    print(url)
                    
                    
                    if self.url.contains("?") {
                        self.url = self.url + ("&devtype=1&token=") + (token)
                    } else {
                        self.url = self.url + ("?devtype=1&token=") + (token)
                    }
                } else {}
                
                
                jiaoYIArray.add(url)
                aaa(str: duihuanArray.lastObject as! String)
            }
            
            decisionHandler(.cancel)
            
        } else {
            decisionHandler(.allow)
            
        }
    }
    
    //url---
    func aaa(str : String) -> Void {
        
        
        let vvv = ChangedRecordVC()
        vvv.url = str
        self.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vvv, animated: true)
    }
}
